var api = require('../../utils/api.js')
var app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        types:[],
        type_name:'',
        type_id:'',
        name:'',
        img:'/images/icons/logo.jpg',
        imgs:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getalltypes();
    },

    /**
     * 上传图片
     */
    uploadimg(){
        let that = this;
        var usrl=api.HOST;
        var imgs=this.data.imgs;

        wx.chooseImage({
          count: 1,
          sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
          sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
          success: function (res) {
            console.log(res)
      
            // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
            let tempFilePaths = res.tempFilePaths;
         
            wx.uploadFile({
              filePath: tempFilePaths[0],
              name: 'file',
              url: usrl+'/api/common/upload',
              formData:{},
              success:function(res){
                var data= JSON.parse(res.data);
                var img=data.data.fullurl;
                console.log(img)
                imgs.push(img);
                that.setData({
                    imgs:imgs
                });
        
              },
              fail: function (res) {
                console.log('上传失败');
                }
            })
          }
        })
    },

    /**
     *  获取分类
     */
    getalltypes(){
        var that=this;
        api.post({
            url: 'orderlist/getalltypes',
            data: {},
            success: data => {
                var info=data.data;
                console.log(info) 
                that.setData({
                    types:info
                })

            }
        })


    },

    //选择分类
    bindPickerChange(ret){
        console.log(ret);
        var index=ret.detail.value;
        var types=this.data.types;
        this.setData({
            type_name:types[index].name,
            type_id:types[index].id,
        })
    },
    getalldesc(ret){
        this.setData({
            name:ret.detail.value
        })
    },
    deleteimg(ret){
        console.log(ret);
        var i=ret.currentTarget.dataset.i;
        var imgs=this.data.imgs;
        var imgb=[];
        imgs.forEach(function(index,item){
            console.log(index,item)
            if(i!=item){
                imgb.push(index)
            }
        })

        this.setData({
            imgs:imgb
        })

    },
    submits(){

        var name=this.data.name;
        var imgs=this.data.imgs;
        var types_id=this.data.type_id;
        var that=this;

        api.post({
            url: 'orderlist/userapply',
            data: {'name':name,imgs:imgs,types_id:types_id},
            success: data => {
                
                console.log(data) 
                wx.showToast({
                  title: data.msg,
                  icon:'none'
                })
                if (data.code==1) {
                    wx.reLaunch({
                      url: '/pages/list/index',
                    })
                }
              

            }
        })


    }

})